var dictForm;
$(function () {

    var columns = [
        {
            field: 'ck',
            checkbox: true
        },
        {
            field: 'dictName',
            title: '字典名称'
        },
        {
            field: 'dictType',
            title: '字典类型'
        },
        {
            field: 'dictLabel',
            title: '字典标签'
        },
        {
            field: 'dictValue',
            title: '字典值'
        },
        {
            field: 'sort',
            title: '排序'
        },
        {
            field: 'status',
            title: '状态',
            align: 'center',
            formatter: function (value, row, index) {
                if (row.status == null || row.status == '1') {
                    return '<button id="btn_edit" type="button" class="btn btn-warning disabled btn-xs" >停用</button>'
                } else {
                    return '<button id="btn_edit" type="button" class="btn btn-success disabled btn-xs" >启用</button>'
                }
            }
        },
        {
            field: 'createDate',
            title: '创建时间',
            formatter: function (value, row, index) {
                return changeDateFormat("yyyy-MM-dd hh:mm:ss", new Date(value))
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row, index) {
                return '<button id="btn_edit" type="button" class="btn btn-primary  btn-xs" onclick="update(\'' + value + '\')"><i class="fa fa-edit"></i> 修改</button>'+
                    '<button type="button" class="btn btn-danger  btn-xs" onclick="deletes(\'' + value + '\')" style="margin-left: 10px;"><i class="fa fa-trash-o"></i> 删除</button>';
            }
        }
    ]
    createBootstrapTable($("#table"), '/sys/sysDict/info', columns, true, 'toolbar', 'client')

})

// 清空表单
function resetForm() {
    $("#dictName").val("")
    $("#dictType").val("")
    $("#dictLabel").val("")
    $("#dictValue").val("")
    $("#sort").val("")
    $("#remarks").val("")
}

function add() {
    resetForm()
    dictForm = layer.open({
        type: 1,
        area: (window.innerWidth>992)? "600px":"auto",
        title: '新增',
        shade: 0.6,
        anim: 1,
        content: $("#dict_form"),
        success: function (layero, index) {
            layer.iframeAuto(index);
        }
    });
}

function save() {
    var id = $("#dictId").val()
    var dictName = $("#dictName").val();
    var dictType = $("#dictType").val();
    var dictLabel = $("#dictLabel").val();
    var dictValue = $("#dictValue").val();
    var sort = $("#sort").val();
    var remarks = $("#remarks").val();
    var status = $("input[name='status']:checked").val();

    if (dictName == '' || dictName  == null) {
        layer.msg("请输入名称！")
        return
    }
    if (dictType == '' || dictType == null) {
        layer.msg("请输入类型！")
        return
    }
    if (dictLabel == '' || dictLabel == null) {
        layer.msg("请输入标签！")
        return
    }
    if (dictValue == '' || dictValue == null) {
        layer.msg("请输入字典值！")
        return
    }

    var data = {
        id: id,
        dictName: dictName,
        dictType: dictType,
        dictLabel: dictLabel,
        dictValue: dictValue,
        sort: sort,
        status:status,
        remarks: remarks
    }
    request('/sys/sysDict/save', 1, data, function (res) {
        if (res.code == "0") {
            layer.close(dictForm)
            layer.msg("保存成功", {time: 1000}, function () {
                onLoads($("#href").val())
            })
        }else {
            layer.msg(res.msg, {time: 1000,icon:5})
        }
    }, function (error) {
        layer.msg("系统异常！")
    })
}

function update(id) {
    resetForm()
    var data = {
        id: id
    }
    request('/sys/sysDict/get', 0, data, function (data) {

        $("#dictId").val(data.id);
        $("#dictName").val(data.dictName);
        $("#dictType").val(data.dictType);
        $("#dictLabel").val(data.dictLabel);
        $("#dictValue").val(data.dictValue);
        $("#sort").val(data.sort);
        $("#remarks").val(data.remarks);
        var r = data.status
        $("input[name='status'][value=" + r + "]").attr("checked", true);
    }, function (error) {
        layer.msg("系统异常！")
    })
    dictForm = layer.open({
        type: 1,
        area: (window.innerWidth>992)? "600px":"auto",
        title: '修改',
        shade: 0.6,
        anim: 1,
        content: $("#dict_form"),
        success: function (layero, index) {
            layer.iframeAuto(index);
        }
    });
}

function deletes(id) {

    var ids = [];
    if (id != undefined) {
        ids.push(id)
    } else {
        $.each($('tbody input:checkbox:checked'), function () {
            ids.push($(this).parents('tr').data('uniqueid'));
        });
    }

    if (ids.length < 1) {
        layer.msg("最少请选择一条数据！")
        return;
    }
    var data = {
        ids: ids.toString()
    }

    layer.confirm('确定删除吗？', {
        title:'提示',btn: ['取消', '确定'] //按钮
    }, function () {
        layer.close(layer.index)
    }, function () {
        request('/sys/sysDict/delete', 1, data, function (res) {
            if (res.code == 0) {
                layer.msg('删除成功', {time: 1000}, function () {
                    onLoads($("#href").val())
                })
            } else {
                layer.msg(res.msg, {time: 1000})
            }

        }, function (error) {
            layer.msg("系统异常！")
        })
    });
}

function searchForm() {
    var searchName = $("#searchName").val()

    var data = {
        postName: searchName
    }

    request('/sys/sysDict/info', 1, data, function (res) {
        $("#table").bootstrapTable('load', res);
    }, function (error) {
        layer.msg(error.msg)
    })
}

function restForm() {
    request('/sys/sysDict/info', 1, null, function (res) {
        $("#table").bootstrapTable('load', res);
    }, function (error) {
        layer.msg(error.msg)
    })
}